<?php
	require_once("../classes/Database.php");
	require_once("../classes/User.php");
	
	$db = new Database("localhost", "root", "");
	$user = new User();
	
    session_start();
	
	if(!empty($_POST["user"]) && !empty($_POST["pass"])){
		$username = $_POST["user"];
		$password = $_POST["pass"];
		$encryptPass = md5($password);
		
		$db->connect("emprego");
		$db->executeQuery("SELECT * FROM users WHERE username='$username' AND password='$encryptPass'");
		
		if($line = $db->getResultLine(MYSQL_ASSOC)){
			$user->setId($line["id"]);
			$user->setName($line["nome"]);
			$user->setUsername($line["username"]);
			$user->setPassword($line["password"]);
			$user->setUpdateDate($line["update_date"]);
			$user->setSaveDate($line["save_date"]);
			$user->setEmail($line["email"]);
			$user->setRole($line["roles"]);
			$user->setSuper($line["is_super"]);
			$_SESSION["user"] = $user;
			$_SESSION["login"] = TRUE;
		}else{
			$_SESSION["user"] = "";
			$_SESSION["login"] = FALSE;
		}
		
		$db->disconnect();
	}else{
		$_SESSION["user"] = "";
		$_SESSION["login"] = FALSE;
	}
	
	header("Location: ../home.php");
?>